﻿#include "QtRender.h"

QtRender::QtRender(RENDER_TYPE Type,int Width,int Height):Render(Type,Width,Height)
{

}

int QtRender::Draw(Frame *frame)
{
    ScalerFrame(frame,AV_PIX_FMT_RGB24);
    QImage imageTmp =  QImage((uint8_t *)dst_video_frame.data[0],
                              m_ImgWidth, m_ImgHeight, QImage::Format_RGB888);
    Img = imageTmp.copy(0, 0, m_ImgWidth, m_ImgHeight);

    update();
    return 0;
}

void QtRender::ScalerFrame(const Frame *Frame, AVPixelFormat Pixel)
{
    if(!img_scaler || req_resize)
    {
        if(img_scaler)
        {
            DeInit();
        }
        m_WinWidth = width();
        m_WinHeight = height();
        m_VideoWidth = Frame->width;
        m_VideoHeight = Frame->height;
        img_scaler = new ImageScaler();
        double video_aspect_ratio = Frame->width * 1.0 / Frame->height;
        double win_aspect_ratio = m_WinWidth * 1.0 / m_WinHeight;
        if(win_aspect_ratio > video_aspect_ratio)
        {
            //此时应该是调整x的起始位置，以高度为基准
            m_ImgHeight = m_WinHeight;
            m_ImgHeight &= 0xfffc;
            m_ImgWidth = m_ImgHeight * video_aspect_ratio;
            m_ImgWidth &= 0xfffc;
            y = 0;
            x = (m_WinWidth - m_ImgWidth) / 2;
        }
        else
        {
            //此时应该是调整y的起始位置，以宽度为基准
            m_ImgWidth = m_WinWidth;
            m_ImgWidth &= 0xfffc;
            m_ImgHeight = m_ImgWidth / video_aspect_ratio;
            m_ImgHeight &= 0xfffc;
            x = 0;
            y = (m_WinHeight - m_ImgHeight) / 2;
        }
        if(Pixel == AV_PIX_FMT_NONE)
        {
            Pixel = (AVPixelFormat)Frame->format;
        }
        img_scaler->Init(m_VideoWidth, m_VideoHeight, Frame->format,
                          m_ImgWidth, m_ImgHeight, Pixel);
        memset(&dst_video_frame, 0, sizeof(VideoFrame));
        dst_video_frame.width = m_ImgWidth;
        dst_video_frame.height = m_ImgHeight;
        dst_video_frame.format = AV_PIX_FMT_RGB24;
        dst_video_frame.data[0] = (uint8_t*)malloc(m_ImgWidth * m_ImgHeight * 3);
        dst_video_frame.linesize[0] = m_ImgWidth * 3; // 每行的字节数
        req_resize = false;
    }
    img_scaler->Scale3(Frame, &dst_video_frame);
}

void QtRender::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    if(m_ImgWidth == 0 || m_ImgHeight == 0)
    {
        QPainter painter(this);
        painter.setPen(QPen(Qt::black, 2)); // 设置画笔颜色和宽度
        painter.drawRect(0, 0, width() - 1, height() - 1); // 绘制矩形边框
    }

    if(play_state == 1)
    {
        // 播放状态
        if (Img.isNull())
        {
            return;
        }
        QPainter painter(this);
        painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
        QRect rect = QRect(x, y, Img.width(), Img.height());
        painter.drawImage(rect, Img.scaled(Img.width(), Img.height()));
    }
    else if(play_state == 2)
    {
        QPainter p(this);
        p.setPen(Qt::NoPen);
        p.setBrush(Qt::black);
        p.drawRect(rect());
    }
}

void QtRender::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    if(m_WinWidth != width() || m_WinHeight != height()) {
        req_resize = true;
    }
}

QtRender::~QtRender()
{
    DeInit();
}
